<template>
  <div class="list-container">
    <el-form class="search-panel" v-model="formData" :inline="true">
      <el-form-item
        label-width="100px"
        class="search-time"
        label="考勤时间："
        prop="time"
      >
        <el-date-picker
          style="width: 250px"
          v-model="formData.startToEnd"
          type="monthrange"
          range-separator="~"
          start-placeholder="开始月份"
          end-placeholder="结束月份"
          value-format="yyyy-MM"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="table-panel">
      <el-table
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" align="center" width="55" label="序号">
        </el-table-column>
        <el-table-column prop="attendanceDate" label="考勤时间" align="center">
        </el-table-column>
        <el-table-column prop="attendanceConfirmedNum" label="工资单已生成人数" align="center">
        </el-table-column>
        <el-table-column prop="referenceMonthlyWageTotal" label="参考月工资" align="center">
          <template slot-scope="scope">
            <div>￥{{scope.row.referenceMonthlyWageTotal}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="actualMonthWageTotal" label="实际月工资" align="center">
          <template slot-scope="scope">
            <div>￥{{scope.row.actualMonthWageTotal}}</div>
          </template>
        </el-table-column>
<!--        <el-table-column prop="status" label="审核状态" align="center">-->
<!--        </el-table-column>-->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="checkRow(scope)"
              type="text"
              icon="el-icon-search"
              >查看</el-button
            >
            <el-button
              :ref="'btn'+scope.$index"
              @click.native.prevent="dailyexport(scope.row)"
              type="text"
              icon="el-icon-download"
              >导出</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-row class="page">
        <div class="page-tag">
          共{{ Math.ceil(totalData / pageSize) }}页/{{ totalData }}条数据
        </div>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-sizes="[10, 20, 50]"
          :page-size="pageSize"
          layout="sizes, prev, pager, next, jumper"
          :total="totalData"
        >
          >
        </el-pagination>
      </el-row>
    </div>
  </div>
</template>

<script>
import { monthWageList,exportMonthPaySlip } from "@/api/monthlyWage";
import workflowApi from '@/api/workflow/workFlow'
import corp from'@/api/corp.js'

export default {
  data() {
    return {
      formData: {
        startToEnd: null,
        status: -1,
        mode: -1,
      },
      corporationId:0,                                    //在项目方登录的时候，选择显示劳务公司的数据时 生效~
      companyId:localStorage.getItem("companyId"),   //这里劳务公司是 劳务公司人员登录进来时 显示该劳务公司的信息时 生效~
      currentPage: 1,
      pageSize: 10,
      totalData: 0,
      tableData: [],
      CompanyApplyList:[]
    };
  },
   created(){
    this.getStatisticList()
  },
  methods: {
     getStatisticList(){
         monthWageList({
          currentPage:this.currentPage,
          pageSize:this.pageSize,
          startDate:this.formData.startToEnd==null?null:this.formData.startToEnd[0],
          endDate:this.formData.startToEnd==null?null:this.formData.startToEnd[1],
          serviceCorporationId: this.companyId
        }).then(res=>{
          this.totalData = res.data.data.total
          this.tableData=res.data.data.records
        })
      },
    onSubmit() {
      this.getStatisticList()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getStatisticList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getStatisticList()
    },
    checkRow(res){
      console.log(res.row)
      this.$router.push({path:'/wage/month/list',query:{date:res.row.attendanceDate}})
    },
    handleSelectionChange(){},
    jump(){
      this.$router.push({path:'/wage/month/list'})
    },
    dailyexport(row){
      exportMonthPaySlip({date:row.attendanceDate}).then(res=>{
        let blob = new Blob([res]);//response.data为后端传的流文件
        console.log(blob);
        console.log(res.data);
        let downloadFilename = row.attendanceDate+"工资单.xlsx";//设置导出的文件名  用moment时间插件对文件命名防止每次都是一样的文件名
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        //兼容ie浏览器
          window.navigator.msSaveOrOpenBlob(blob)
        }else {
        //谷歌,火狐等浏览器
          let url = window.URL.createObjectURL(blob);
          let downloadElement = document.createElement("a");
          downloadElement.style.display = "none";
          downloadElement.href = url;
          downloadElement.download = downloadFilename;
          document.body.appendChild(downloadElement);
          downloadElement.click();
          document.body.removeChild(downloadElement);
          window.URL.revokeObjectURL(url);
        }
      }).catch(function (err) {
        this.$message.error(err)
        console.log(err)
      }).finally(function () {
          // this.tableLoading = false;
      })
    },
  },
}
</script>
